import router from '../router/index'
import qs from 'qs'

const contentTypeForm = 'application/x-www-form-urlencoded;charset=UTF-8'
const contentTypeJson = 'application/json'
export default function(config) {
  const url = config.url
  let params = config.params || {}
  let dataType = config.dataType == undefined ? 'form' : config.dataType
  let showLoading = config.showLoading
  showLoading = showLoading == undefined ? true : showLoading
  let loading = null
  let showLoginOut =
    config.showLoginOut == undefined ? true : config.showLoginOut

  // create an axios instance
  axios.interceptors.request.use(
    (config) => {
      config.headers.post['Content-Type'] =
        dataType == 'form' ? contentTypeForm : contentTypeJson
      config.headers.post['X-Requested-With'] = 'XMLHttpRequest'
      config.withCredentials = false
      config.baseURL = '/api'
      return config
    },
    (error) => {
      // do something with request error
      return Promise.reject(error)
    }
  )
  return new Promise((resolve, reject) => {
    if (showLoading) {
      loading = this.$loading({
        lock: true,
        spinner: 'el-icon-customer',
        background: 'hsla(0,0%,100%,.4)',
      })
    }

    for (let item in params) {
      if (params[item] && typeof params[item] == 'string') {
        params[item] = params[item].trim()
      }
    }

    params = dataType == 'form' ? qs.stringify(params) : params

    axios
      .post(url, params, {
        headers: {},
      })
      .then(
        (response) => {
          if (showLoading) {
            loading.close()
          }
          const responseData = response.data
          if (responseData.code == 200) {
            return resolve(responseData.data)
          } else if (responseData.code == 901) {
            // 登录超时
            sessionStorage.clear()
            if (showLoginOut) {
              this.$message.error(responseData.info)
              this.$message({
                message: responseData.info,
                type: 'error',
                duration: 1000,
                onClose: () => {
                  router.push('/login')
                },
              })
            } else {
              router.push('/login')
            }
          } else if (responseData.code == 902) {
            // 没权限
            this.$message({
              message: responseData.info,
              type: 'error',
              duration: 1000,
              /*  onClose: () => {
                router.push('/pageNoPermission')
              }, */
            })
          } else {
            if (config.errorCallback) {
              config.errorCallback()
            }
            return this.$message.error(responseData.info)
          }
        },
        (error) => {
          if (showLoading) {
            loading.close()
          }
          this.$message.error('网络异常')
          reject(error)
        }
      )
      .catch((error) => {
        if (showLoading) {
          loading.close()
        }
        this.$message.error('网络异常')
        reject(error)
      })
  })
}
